<template>
  <div class="execution">
    <basic-container>
      <avue-crud :before-open="beforeOpen" ref="crud" :page.sync="page" :data="tableData" v-model="tableForm" :permission="permissionList" :table-loading="tableLoading" :option="tableOption" @on-load="getList" @search-change="searchChange" @refresh-change="refreshChange" @size-change="sizeChange" @current-change="currentChange" @row-update="handleUpdate" @row-save="handleSave" @row-del="handleDel">
        <template slot="menuLeft">
          <el-button class="filter-item" v-if="permissions.mall_imagesgroup_del" plain type="danger" size="small" icon="el-icon-remove" @click="handleDels">批量删除</el-button>
          <el-button v-if="permissions.mall_imagesgroup_export" class="filter-item" plain type="primary" size="small" icon="el-icon-download" @click="exportExcel">导出</el-button>
        </template>
        <!-- 使用自定义Form -->
        <template slot="groupIdForm" slot-scope="{type,disabled}">
          <!-- 只新增、编辑添加“新增图片组”按钮 -->
          <el-button class="filter-item" plain type="primary" size="small" icon="el-icon-plus" v-if="type=='add'||type=='edit'" :disabled="hasAddMax" @click="addNewGroupId">使用新的图片组</el-button>
          <el-select :disabled="disabled" v-model="tableForm.groupId" v-if="type=='add'||type=='edit'">
            <el-option v-for="item in groupIdOptions" :key="item.groupId" :label="item.groupId" :value="item.groupId"></el-option>
          </el-select>
          <el-select :disabled="disabled" v-model="tableForm.groupId" v-else>
            <el-option v-for="item in groupIdOptions" :key="item.groupId" :label="item.groupId" :value="item.groupId"></el-option>
          </el-select>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, getObj, addObj, putObj, delObj, delObjs, groupIdList } from '@/api/mall/imagesGroup'
import { tableOption } from '@/const/crud/mall/imagesGroup'
import { mapGetters } from 'vuex'

export default {
  name: 'imagesgroup',
  data() {
    return {
      searchForm: {},
      tableData: [],
      tableForm: {},
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,
      groupIdOptions: [],
      hasAddMax: false
    }
  },
  mounted() {
    this.initGroupIdList()
  },
  computed: {//计算属性对象
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        viewBtn: this.vaildData(this.permissions.mall_imagesgroup_view, false),
        addBtn: this.vaildData(this.permissions.mall_imagesgroup_add, false),
        delBtn: this.vaildData(this.permissions.mall_imagesgroup_del, false),
        editBtn: this.vaildData(this.permissions.mall_imagesgroup_edit, false)
      };
    }
  },
  methods: {
    beforeOpen(done, type) {
      if (["add"].includes(type)) {
        this.hasAddMax = false
      }
      done();
    },
    //初始化全部组id
    initGroupIdList() {
      var _this = this;
      groupIdList().then(response => {
        if (response.data.data.length == 0) {
          _this.groupIdOptions.push(max);
        } else {
          _this.groupIdOptions = response.data.data
        }
      })
    },
    //使用新的图片组
    addNewGroupId() {
      if (this.hasAddMax) {
        return
      }
      this.tableForm.groupId = Math.max(this.groupIdOptions) + 1
      this.hasAddMax = true
    },
    // 列表查询
    getList(page, params) {
      this.tableLoading = true
      fetchList(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm)).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    // 删除
    handleDel: function(row, index) {
      this.$confirm('是否确认删除ID为' + row.igid, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delObj(row.igid)
      }).then(data => {
        this.$message.success('删除成功')
        this.getList(this.page)
      })
    },
    handleDels() {
      if (this.$refs.crud.tableSelect.length === 0) {
        this.$message.error('请选择后进行删除')
        return false
      }
      let Ids = []
      for (const row of this.$refs.crud.tableSelect) {
        Ids.push(row.igid)
      }
      delObjs(Ids).then(() => {
        this.$notify.success('删除成功')
        this.$refs.crud.toggleSelection();
        this.refreshChange();
      })
    },
    // 更新
    handleUpdate: function(row, index, done, loading) {
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading();
      });
    },
    // 保存
    handleSave: function(row, done, loading) {
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading();
      });
    },
    // 每页条数改变事件
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    // 当前页发生改变事件
    currentChange(current) {
      this.page.currentPage = current
    },
    // 查询事件
    searchChange(form, done) {
      this.searchForm = form
      this.page.currentPage = 1
      this.getList(this.page, form)
      done()
    },
    // 刷新事件
    refreshChange() {
      this.getList(this.page)
    },
    //  导出excel
    exportExcel() {
      this.downBlobFile('/admin/mall/imagesGroup/export', this.searchForm, 'imagesgroup.xlsx')
    }
  }
}
</script>
<style lang="scss" scoped>
  .el-tag--small {
    height: 32px !important;
  }
</style>
